package vn.allmobile.ambnews;

import vn.allmobile.ambnews.network.SynchronizeService;
import vn.allmobile.ambnews.utils.Log;
import vn.allmobile.ambnews.utils.PreferenceManager;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockPreferenceActivity;
import com.actionbarsherlock.view.MenuItem;

import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.preference.PreferenceGroup;
import android.preference.PreferenceScreen;
import android.widget.Toast;

public class SettingsActivity extends SherlockPreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener{

	

	
	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.settings);  
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getSupportActionBar().setDisplayHomeAsUpEnabled(false);
		}
		
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	   switch (item.getItemId()) {
	      case android.R.id.home:
	    	  Log.v("home press");
	    	  onBackPressed();	         
	         return true;
	   }
	   return super.onOptionsItemSelected(item);
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		Log.v("key:"+ key);
		if ("auto_update".equals(key))
	    {
	      Log.v("auto_update" + PreferenceManager.isUpdateEnable());
	      SynchronizeService.nextTimeStart();
	    }
		if ("night_mode".equals(key))
	    {	     
	      Toast.makeText(this,"Chế độ đọc ngày/đêm đã thay đổi. Vui lòng khởi động lại ứng dụng để thực hiện chức năng này.",Toast.LENGTH_LONG).show();
	    }
		Log.v("getIntervalUpdateTime" + PreferenceManager.getIntervalUpdateTime());
		Log.v("isStopInSleep" + PreferenceManager.isStopInSleep());
		Preference localPreference = findPreference(key);
	      if (localPreference instanceof ListPreference)
	        localPreference.setSummary(((ListPreference)localPreference).getEntry());
	}

	
	/**
	    * Set the summaries of all preferences
	    */
	  private void initSummaries(PreferenceGroup pg) {
	    for (int i = 0; i < pg.getPreferenceCount(); ++i) {
	    Preference p = pg.getPreference(i);
	    if (p instanceof PreferenceGroup)
	      this.initSummaries((PreferenceGroup) p); // recursion
	    else
	      this.setSummary(p);
	    }
	  }

	  /**
	    * Set the summaries of the given preference
	    */
	  private void setSummary(Preference pref) {
	    // react on type or key
	      if (pref instanceof ListPreference) {
	      ListPreference listPref = (ListPreference) pref;
	      pref.setSummary(listPref.getEntry());
	      }
	  }
	
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
	    

	    return true;
	}
	
	@Override
	 protected void onPause()
	  {
	    super.onPause();
	    getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
	  }
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
		//updatePreference(getPreferenceScreen());
		initSummaries(getPreferenceScreen());
	}
	
}
